package com.rockcent.api.controller;

import com.rockcent.api.Routes;
import com.rockcent.api.helper.UserHelper;
import com.rockcent.common.common.JsonResult;
import com.rockcent.common.common.PageResult;
import com.rockcent.common.common.RestDoing;
import com.rockcent.common.controller.BaseController;
import com.rockcent.mall.lib.domain.MallMemberBaseServiceFeeFlow;
import com.rockcent.mall.lib.domain.MallMemberMerchantServiceFeeFlow;
import com.rockcent.mall.lib.domain.MallMemberRechargeFlow;
import com.rockcent.mall.lib.domain.MallMemberTransactionCommissionFlow;
import com.rockcent.mall.lib.domain.enumclass.MallMemberFundTransactionType;
import com.rockcent.mall.lib.dto.MallMemberFundFlowDto;
import com.rockcent.mall.lib.dto.UserInfoDto;
import com.rockcent.service.*;
import org.apache.commons.lang3.EnumUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 会员账号资金记录
 * Created by zhangjh on 2017/3/9.
 */
@RestController
@RequestMapping(value = Routes.API_VERSION)
public class MallMemberFundFlowController extends BaseController {
    private final Logger log = LoggerFactory.getLogger(MallMemberFundFlowController.class);

    @Autowired
    private UserHelper userHelper;
    @Autowired
    private MallMemberFundFlowSrv mallMemberFundFlowSrv;
    @Autowired
    private MallMemberRechargeFlowSrv mallMemberRechargeFlowService;
    @Autowired
    private MallMemberBaseServiceFeeFlowSrv mallMemberBaseServiceFeeFlowSrv;
    @Autowired
    private MallMemberTransactionCommissionFlowSrv mallMemberTransactionCommissionFlowSrv;
    @Autowired
    private MallMemberMerchantServiceFeeFlowSrv mallMemberMerchantServiceFeeFlowSrv;

    /**
     * @api {get} /v1/mall/member/fund/flow/page M 会员账户资金记录分页
     * @apiGroup mall_member
     * @apiVersion 1.0.0
     * @apiHeader {String} userToken 用户token
     * @apiHeader {String} domain    请求域名
     * @apiParam {String} transactionType <code>必选参数</code> 交易类型 ： ALL("全部类型"),OUTLINE_RECHARGE("线下充值"),RECHARGE_PRESENTED("充值赠送"),BASE_SERVICE_PAYMENTS("基础服务费扣费"), MALL_EXCHANGE("平台交易佣金扣费"), MERCHANT_SERVICE_PAYMENTS("商户服务费扣费"), RECHARGE_COMMON("普通充值")
     * @apiParam {String} seqNo <code>可选参数</code> 流水号
     * @apiParam {String} operationAmount <code>可选参数</code> 金额
     * @apiParam {String} beginDate <code>可选参数</code> 提交开始时间
     * @apiParam {String} endDate <code>可选参数</code> 提交结束时间
     *
     * @apiSuccess {long} id id
     * @apiSuccess {long} seqNo 流水号
     * @apiSuccess {long} mallId 平台id
     * @apiSuccess {String} mallName 平台名称
     * @apiSuccess {long} mallMemberId 会员账号id
     * @apiSuccess {String} transsactionType 交易类型
     * @apiSuccess {BigDecimal} operationAmount 金额
     * @apiSuccess {BigDecimal} balance 账户余额
     * @apiSuccess {String} remark 流水备注
     *
     *
     * @apiSuccessExample {json} Success-Response:
     * {
     *     "code":"1",
     *     "desc":"成功",
     *     "data":{
     *         "data":[
     *             {
     *                 "id":1,
     *                 "dateCreated":"2017-03-10 14:02:49",
     *                 "lastUpdated":"2017-03-10 14:02:49",
     *                 "seqNo":"001489117673114",
     *                 "mallId":51,
     *                 "mallName":"络享内测平台",
     *                 "mallMemberId":1,
     *                 "transactionType":"OUTLINE_RECHARGE",
     *                 "operationAmount":10000,
     *                 "balance":10000,
     *                 "remark":"线下充值"
     *             },
     *             {
     *                 "id":6,
     *                 "dateCreated":"2017-03-10 18:26:01",
     *                 "lastUpdated":"2017-03-10 18:26:01",
     *                 "seqNo":"001489141621045",
     *                 "mallId":51,
     *                 "mallName":"络享内测平台",
     *                 "mallMemberId":1,
     *                 "transactionType":"OUTLINE_RECHARGE",
     *                 "operationAmount":17.28,
     *                 "balance":9963.44,
     *                 "remark":"商户服务费扣费"
     *             },
     *             {
     *                 "id":9,
     *                 "dateCreated":"2017-03-10 18:31:04",
     *                 "lastUpdated":"2017-03-10 18:31:04",
     *                 "seqNo":"001489141923594",
     *                 "mallId":51,
     *                 "mallName":"络享内测平台",
     *                 "mallMemberId":1,
     *                 "transactionType":"RECHARGE_PRESENTED",
     *                 "operationAmount":55,
     *                 "balance":10056.16,
     *                 "remark":"充值赠送"
     *             },
     *             {
     *                 "id":11,
     *                 "dateCreated":"2017-03-10 18:33:40",
     *                 "lastUpdated":"2017-03-10 18:33:40",
     *                 "seqNo":"001489142079985",
     *                 "mallId":51,
     *                 "mallName":"络享内测平台",
     *                 "mallMemberId":1,
     *                 "transactionType":"RECHARGE_COMMON",
     *                 "operationAmount":1000,
     *                 "balance":12056.16,
     *                 "remark":"普通充值"
     *             }
     *         ],
     *         "totalCount":18,
     *         "totalPage":2,
     *         "pageNo":0,
     *         "pageSize":10,
     *         "url":"/mall/member/fund/flow/page",
     *         "prevUrl":"/mall/member/fund/flow/page?pageNo=0&pageSize=10",
     *         "nextUrl":"/mall/member/fund/flow/page?pageNo=1&pageSize=10"
     *     }
     * }
     *
     *
     */
    @RequestMapping(value = Routes.MALL_MEMBER_FUND_FLOW_PAGE, method = RequestMethod.GET)
    public JsonResult pageByMallId(MallMemberFundFlowDto dto, HttpServletRequest request) {
        UserInfoDto userDto = userHelper.getSession(request);
        Long mallId = userDto.mall.getId();

        RestDoing doing = jsonResult -> {
            if (null == dto) {
                jsonResult.errorParam("查询条件为空", log);
                return;
            }
            if (!EnumUtils.isValidEnum(MallMemberFundTransactionType.class, dto.getTransactionType())) {
                jsonResult.errorParam("交易类型为空", log);
                return;
            }
            dto.setMallId(mallId);

            PageResult<MallMemberFundFlowDto> pageResult = mallMemberFundFlowSrv.findMallMemberFundFlowPage(dto, getPage(dto.getPageNo(), dto.getPageSize()));
            pageResult.setUrl(Routes.MALL_MEMBER_FUND_FLOW_PAGE);

            jsonResult.data = pageResult;
        };
        return doing.go(request, log);
    }


    /**
     * @api {get} /v1/mall/member/fund/flow/info R 会员账户资金交易详情
     * @apiGroup mall_member
     * @apiVersion 1.0.0
     * @apiHeader {String} userToken 用户token
     * @apiHeader {String} domain    请求域名
     * @apiParam {String} transactionType <code>必选参数</code> 交易类型 ： ALL("全部类型"),OUTLINE_RECHARGE("线下充值"),RECHARGE_PRESENTED("充值赠送"),BASE_SERVICE_PAYMENTS("基础服务费扣费"), MALL_EXCHANGE("平台交易佣金扣费"), MERCHANT_SERVICE_PAYMENTS("商户服务费扣费"), RECHARGE_COMMON("普通充值")
     * @apiParam {String} seqNo <code>必选参数</code> 流水号
     *
     * @apiSuccess {String} id ID
     * @apiSuccess {String} dateCreated 更新时间
     * @apiSuccess {String} seqNo 流水号
     * @apiSuccess {long} mallId 平台id
     * @apiSuccess {long} mallMemberId 会员id
     * @apiSuccess {String} rechargeAmount 汇款金额
     * @apiSuccess {String} dateCreated 提交时间
     * @apiSuccess {String} lastUpdated 充值成功时间/扣费时间
     * @apiSuccess {String} rechargeAccount 汇款账号
     * @apiSuccess {String} rechargeDate 汇款账号
     * @apiSuccess {String} rechargeVoucher 汇款凭证
     * @apiSuccess {String} auditStatus 审核状态
     * @apiSuccess {String} settlementDate 结算日
     * @apiSuccess {BigDecimal} amount 金额
     * @apiSuccess {BigDecimal} baseServiceFeeNum 基础服务费系数
     * @apiSuccess {BigDecimal} transactionAmount 交易总额
     * @apiSuccess {BigDecimal} commissionRate 交易佣金率
     * @apiSuccess {long} merchantCounts 结算日商户数
     * @apiSuccess {BigDecimal} businessServiceFee 商户服务费/户
     * @apiSuccess {BigDecimal} businessServiceFeeNum 商户服务费系数
     *
     * @apiSuccessExample {json} 充值详情
     * {
     *   "code": "1",
     *   "desc": "成功",
     *   "data": {
     *     "id": 2,
     *     "dateCreated": "2017-03-10 11:46:54",
     *     "lastUpdated": "2017-03-10 14:02:49",
     *     "version": 2,
     *     "seqNo": "001489117673114",
     *     "mallId": 51,
     *     "mallMemberId": 1,
     *     "rechargeAmount": 10000,
     *     "rechargeAccount": "6228480402564890018",
     *     "rechargeDate": "2016-03-10",
     *     "rechargeVoucher": "http://wx.qlogo.cn/mmopen/UOCHvzUGAIV8FXCZV5LEuCyEFNLvYqlw6aicxXiasYKr6qmesZKTiatyujJO42jdfW877UJsmeVj0HwsJRibGIxWSvlAFSiaicAD5V/0",
     *     "auditStatus": "PASS",
     *     "remark": "通过"
     *   }
     * }
     *
     * @apiSuccessExample {json} 基础服务费详情
     * {
     *   "code": "1",
     *   "desc": "成功",
     *   "data": {
     *     "id": 1,
     *     "dateCreated": "2017-03-12 14:39:11",
     *     "lastUpdated": "2017-03-12 15:37:16",
     *     "version": 0,
     *     "seqNo": "001489300811180",
     *     "mallId": 51,
     *     "mallMemberId": 1,
     *     "settlementDate": "2017-03-12 14:40:12",
     *     "amount": 135435.3,
     *     "baseServiceFeeNum": "0.1",
     *     "remark": "扣减平台基础服务费流水"
     *   }
     * }
     *
     * @apiSuccessExample {json} 交易佣金扣费详情
     * {
     *   "code": "1",
     *   "desc": "成功",
     *   "data": {
     *     "id": 1,
     *     "dateCreated": "2017-03-12 14:39:27",
     *     "lastUpdated": "2017-03-12 14:39:27",
     *     "version": 0,
     *     "seqNo": "001489300826298",
     *     "mallId": 51,
     *     "mallMemberId": 1,
     *     "settlementDate": "2017-03-12 14:40:28",
     *     "transactionAmount": 0,
     *     "commissionRate": 1,
     *     "amount": 0,
     *     "remark": "扣减平台交易佣金流水"
     *   }
     * }
     * @apiSuccessExample {json} 商户服务费扣费详情
     * {
     *   "code": "1",
     *   "desc": "成功",
     *   "data": {
     *     "id": 2,
     *     "dateCreated": "2017-03-12 14:24:24",
     *     "lastUpdated": "2017-03-12 15:37:29",
     *     "version": 0,
     *     "seqNo": "001489299922085",
     *     "mallId": 51,
     *     "mallMemberId": 1,
     *     "settlementDate": "2017-03-12 14:25:24",
     *     "merchantCounts": 1,
     *     "businessServiceFee": 1.2,
     *     "businessServiceFeeNum": "0.2",
     *     "amount": 17.28,
     *     "remark": "扣减平台商户服务费流水"
     *   }
     * }
     *
     */
    @RequestMapping(value = Routes.MALL_MEMBER_FUND_FLOW_INFO, method = RequestMethod.GET)
    public JsonResult info(MallMemberFundFlowDto dto, HttpServletRequest request) {
//        UserInfoDto userDto = userHelper.getSession(request);
//        Long mallId = userDto.mall.getId();

        RestDoing doing = jsonResult -> {
            if (null == dto) {
                jsonResult.errorParam("查询条件为空", log);
                return;
            }
            String transactionType = dto.getTransactionType();
            if (!EnumUtils.isValidEnum(MallMemberFundTransactionType.class, transactionType)) {
                jsonResult.errorParam("交易类型不对", log);
                return;
            }
            String seqNo = dto.getSeqNo();
            if (StringUtils.isBlank(seqNo)) {
                jsonResult.errorParam("流水号为空", log);
                return;
            }
//            dto.setMallId(mallId);

            if (MallMemberFundTransactionType.OUTLINE_RECHARGE.toString().equals(transactionType)) {
                MallMemberRechargeFlow flow = mallMemberRechargeFlowService.findTop1BySeqNo(seqNo);
                jsonResult.data = flow;
            } else if (MallMemberFundTransactionType.BASE_SERVICE_PAYMENTS.toString().equals(transactionType)) {
                MallMemberBaseServiceFeeFlow flow = mallMemberBaseServiceFeeFlowSrv.findTop1BySeqNo(seqNo);
                jsonResult.data = flow;
            } else if (MallMemberFundTransactionType.MALL_EXCHANGE.toString().equals(transactionType)) {
                MallMemberTransactionCommissionFlow flow = mallMemberTransactionCommissionFlowSrv.findTop1BySeqNo(seqNo);
                jsonResult.data = flow;
            } else if (MallMemberFundTransactionType.MERCHANT_SERVICE_PAYMENTS.toString().equals(transactionType)) {
                MallMemberMerchantServiceFeeFlow flow = mallMemberMerchantServiceFeeFlowSrv.findTop1BySeqNo(seqNo);
                jsonResult.data = flow;
            } else if (MallMemberFundTransactionType.RECHARGE_COMMON.toString().equals(transactionType)) {

            } else if (MallMemberFundTransactionType.RECHARGE_PRESENTED.toString().equals(transactionType)) {


            }


        };
        return doing.go(request, log);
    }



    /**
     * @api {get} /v1/mall/member/fund/flow/page/export  M 导出会员账户资金记录报表
     * @apiGroup mall_member
     * @apiVersion 1.0.0
     * @apiHeader {String} userToken 用户token
     * @apiHeader {String} domain 请求域名
     * @apiParam {String} transactionType <code>必选参数</code> 交易类型 ： ALL("全部类型"),OUTLINE_RECHARGE("线下充值"),RECHARGE_PRESENTED("充值赠送"),BASE_SERVICE_PAYMENTS("基础服务费扣费"), MALL_EXCHANGE("平台交易佣金扣费"), MERCHANT_SERVICE_PAYMENTS("商户服务费扣费"), RECHARGE_COMMON("普通充值")
     * @apiParam {String} seqNo <code>可选参数</code> 流水号
     * @apiParam {String} operationAmount <code>可选参数</code> 金额
     * @apiParam {String} beginDate <code>可选参数</code> 提交开始时间
     * @apiParam {String} endDate <code>可选参数</code> 提交结束时间
     * @apiSuccessExample {json} Success-Response:
     *  {
     *      "code": "1",
     *      "desc": "成功"
     *  }
     */
    @RequestMapping(value = Routes.MALL_MEMBER_FUND_FLOW_PAGE_EXPORT, method = RequestMethod.GET)
    public JsonResult export(MallMemberFundFlowDto dto, HttpServletRequest request,HttpServletResponse response){
        UserInfoDto userInfoDto = userHelper.getSession(request);
        RestDoing doing = jsonResult -> {
            Long mallId = userInfoDto.mall.getId();
            dto.setMallId(mallId);
            mallMemberFundFlowSrv.export(request,response,dto);
        };
        return doing.go(request, log);
    }


    /**
     * @api {get} /v1/mall/member/all/fund/flow/page R 乐宸资金账户记录分页/平台账户资金明细
     * @apiGroup mall_member
     * @apiVersion 1.0.0
     * @apiHeader {String} userToken 用户token
     * @apiHeader {String} domain    请求域名
     * @apiParam {String} transactionType <code>必选参数</code> 交易类型 ： ALL("全部类型"),OUTLINE_RECHARGE("线下充值"),RECHARGE_PRESENTED("充值赠送"),BASE_SERVICE_PAYMENTS("基础服务费扣费"), MALL_EXCHANGE("平台交易佣金扣费"), MERCHANT_SERVICE_PAYMENTS("商户服务费扣费"), RECHARGE_COMMON("普通充值")
     * @apiParam {String} seqNo <code>可选参数</code> 流水号
     * @apiParam {BigDecimal} operationAmount <code>可选参数</code> 金额
     * @apiParam {String} beginDate <code>可选参数</code> 提交开始时间
     * @apiParam {String} endDate <code>可选参数</code> 提交结束时间
     * @apiParam {String} mallId <code>可选参数</code> 平台ID
     * @apiParam {String} mallName <code>可选参数</code>  平台名称
     * @apiSuccess {long} id ID
     * @apiSuccess {long} seqNo 流水号
     * @apiSuccess {long} mallId 平台ID
     * @apiSuccess {String} mallName 平台名称
     * @apiSuccess {String} transactionType 交易类型
     * @apiSuccess {BigDecimal} operationAmount 金额
     * @apiSuccess {BigDecimal} balance 账户余额
     * @apiSuccess {String} lastUpdated 操作时间
     *

     * @apiSuccessExample {json} Success-Response:
     * {
     *   "code": "1",
     *   "desc": "成功",
     *   "data": {
     *     "data": [
     *       {
     *         "id": 1,
     *         "dateCreated": "2017-03-10 14:02:49",
     *         "lastUpdated": "2017-03-10 14:02:49",
     *         "seqNo": "001489117673114",
     *         "mallId": 51,
     *         "mallName": "乐宸",
     *         "mallMemberId": 1,
     *         "transactionType": "OUTLINE_RECHARGE",
     *         "operationAmount": 10000,
     *         "balance": 10000,
     *         "remark": "线下充值"
     *       }
     *     ],
     *     "totalCount": 1,
     *     "totalPage": 1,
     *     "pageNo": 0,
     *     "pageSize": 10,
     *     "url": "/mall/member/all/fund/flow/page",
     *     "prevUrl": "/mall/member/all/fund/flow/page?pageNo=0&pageSize=10",
     *     "nextUrl": "/mall/member/all/fund/flow/page?pageNo=0&pageSize=10"
     *   }
     * }
     */
    @RequestMapping(value = Routes.MALL_MEMBER_ALL_FUND_FLOW_PAGE, method = RequestMethod.GET)
    public JsonResult pageAll(MallMemberFundFlowDto dto, HttpServletRequest request) {
//        UserInfoDto userDto = userHelper.getSession(request);
        RestDoing doing = jsonResult -> {
            if (null == dto) {
                jsonResult.errorParam("查询条件为空", log);
                return;
            }
            if (!EnumUtils.isValidEnum(MallMemberFundTransactionType.class, dto.getTransactionType())) {
                jsonResult.errorParam("交易类型为空", log);
                return;
            }

            PageResult<MallMemberFundFlowDto> pageResult = mallMemberFundFlowSrv.findMallMemberFundFlowPage(dto, getPage(dto.getPageNo(), dto.getPageSize()));
            pageResult.setUrl(Routes.MALL_MEMBER_ALL_FUND_FLOW_PAGE);

            jsonResult.data = pageResult;
        };
        return doing.go(request, log);
    }

}
